//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This utility provides a simple wrapper around the LLVM Execution Engines,
// which allow the direct execution of LLVM programs through a Just-In-Time
// compiler, or through an interpreter if no JIT is available for this platform.
//
//===----------------------------------------------------------------------===//

#ifndef EMULATOR_INTERPRETER_H 
#define EMULATOR_INTERPRETER_H 

#include "instr.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Type.h"
#include "llvm/InstrTypes.h"
#include "llvm/Support/Host.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Function.h"
#include "llvm/Support/IRBuilder.h"
#include "llvm/CodeGen/LinkAllCodegenComponents.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/ExecutionEngine/JITEventListener.h"
#include "llvm/ExecutionEngine/JITMemoryManager.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/TargetSelect.h"
#include <cerrno>

using namespace llvm;

class EmulatorInterpreter 
{
private:
  /// private contor.
  EmulatorInterpreter() {}

public:
  // ========================================
  // callbacks to get service from the interpreter.
  // ========================================
  void *find_field_offset_by_name; 
  void *find_reg_offset_by_index; 
  void *interpret;
  void *naked_interpret;
  void *resolve_modrm;
  void *modrm_xlation;

public:
  // ========================================
  // initialize field offsets for fast access. 
  // ========================================
  uint32_t async_event_offset;
  uint32_t curr_rip_offset;
  uint32_t prev_rip_offset;
  uint32_t address_to_xlate_offset;
  uint32_t address_xlated_offset;

public:
  void initializeFieldOffset(void);

  // ========================================
  // getters for field offsets. 
  // ========================================
  uint32_t get_async_event_offset(void) const { return async_event_offset; }
  uint32_t get_reg_offset(uint8_t index, uint8_t type);

public: 
  /// CreateDefaultIRGenerator - create an emulator ir generator.
  static EmulatorInterpreter *CreateDefaultInterpreter(void) 
  {
    EmulatorInterpreter *NewInt = new EmulatorInterpreter();
    return NewInt;
  }
};

#endif   // EMULATOR_INTERPRETER_H 

